/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package editorRozpisu;

import java.awt.Color;
import java.awt.Component;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import strukturaZamestnanciNepritomnost.Zamestnanci;
import strukturaZamestnanciNepritomnost.Zamestnanec;

public class RendererTypZamestnanca extends DefaultTableCellRenderer {

    Zamestnanci z = null;

    public RendererTypZamestnanca() {
        try {
            z = new Zamestnanci();
        } catch (SQLException ex) {
            Logger.getLogger(RendererTypZamestnanca.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(RendererTypZamestnanca.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
        Component cell = super.getTableCellRendererComponent(
                table, obj, isSelected, hasFocus, row, column);

        int idcko = vratIDzamestnanca(table.getValueAt(row, column).toString());
        if (idcko != -1) {
            if (z.getZamestnanec(vratIDzamestnanca(table.getValueAt(row, column).toString())).getTypZamestnanca() == 0) {
                cell.setBackground(Color.white);
                cell.setForeground(Color.black);
            } else {
                cell.setBackground(Color.lightGray);
                cell.setForeground(Color.black);
            }
        } else {
            cell.setBackground(Color.decode("#FF7A73"));
            cell.setForeground(Color.black);
        }
        return cell;
    }

    private int vratIDzamestnanca(String priezmeno) {
        if (!priezmeno.equals("-")) {
            String priezvisko = priezmeno.substring(0, priezmeno.indexOf(" "));
            String meno = priezmeno.substring(priezmeno.indexOf(" ") + 1);
            for (Map.Entry<Integer, Zamestnanec> elem : z.getZamestnanciAll().entrySet()) {
                int id = elem.getKey();
                Zamestnanec zam = elem.getValue();
                if (zam.getMeno().equals(meno) && zam.getPriezvisko().equals(priezvisko)) {
                    return id;
                }
            }
        }
        return -1;
    }
}